<?php
class UserXtagModel extends Model {	
	protected	$tableName	=	'x_tag';
	function allCount(){
		set_time_limit(0);
		
		$this->updataWikiCount();
		$this->updataAskCount();
		$this->updataBlogCount();
		$this->updataUserCount();
		$this->updataBbsTagCount();
		$this->updataTagCount();		
	}
	
	//更新全部标签统计
	function updataTagCount(){		
		$list = D()->query("SELECT tagid, totalcount, usercount, wikicount, askcount, blogcount, bbscount FROM `ts_x_tag`");
		//dump(D()->getLastSql());exit;
		//dump($list);exit;
		foreach ($list as $v){
			$map['tagid'] = $v['tagid'];
			$data['totalcount'] = $v['usercount']+$v['wikicount']+$v['askcount']+$v['blogcount']+$v['bbscount'];
			
			if($data['totalcount']!=$v['totalcount']){
				$res = M('x_tag')->where($map)->save($data);
				//dump($res);
				//dump(M('x_tag')->getLastSql());
			}
		}
		
		return true;		
	}
	
	//单独更新用户的标签统计
	function updataUserCount(){
		$list = D()->query("SELECT tagid, count( distinct uid) as usercount FROM `ts_space_tag` GROUP BY tagid");
		//dump(D()->getLastSql());exit;
		//dump($list);exit;
		foreach ($list as $v){			
			$map['tagid'] = $v['tagid'];			
			$data['usercount'] = $v['usercount'];

			$res = M('x_tag')->where($map)->save($data);
			//dump($res);
			//dump(M('x_tag')->getLastSql());
		}
	}
	
	//单独更新百科的标签统计
	function updataWikiCount(){
		$list = D()->query("SELECT tagid, count( distinct id) as wikicount FROM `ts_wiki_tag` GROUP BY tagid");
		//dump(D()->getLastSql());exit;
		//dump($list);exit;
		foreach ($list as $v){			
			$map['tagid'] = $v['tagid'];			
			$data['wikicount'] = $v['wikicount'];

			$res = M('x_tag')->where($map)->save($data);
			//dump($res);
			//dump(M('x_tag')->getLastSql());
		}
	}

	//单独更新知道的标签统计
	function updataAskCount(){
		$list = D()->query("SELECT classifyId as tagid, count( distinct askId) as askcount FROM `ts_ask_classify` Where type=1 GROUP BY classifyId");
		//dump(D()->getLastSql());exit;
		//dump($list);exit;
		foreach ($list as $v){			
			$map['tagid'] = $v['tagid'];			
			$data['askcount'] = $v['askcount'];

			$res = M('x_tag')->where($map)->save($data);
			//dump($res);
			//dump(M('x_tag')->getLastSql());
		}
	}
	
	//单独更新博客的标签统计
	function updataBlogCount(){
		$list = D()->query("SELECT tagid, count( distinct blogId) as blogcount FROM `ts_blog_tags` GROUP BY tagId");
		//dump(D()->getLastSql());exit;
		//dump($list);exit;
		foreach ($list as $v){			
			$map['tagid'] = $v['tagid'];			
			$data['blogcount'] = $v['blogcount'];

			$res = M('x_tag')->where($map)->save($data);
			//dump($res);
			//dump(M('x_tag')->getLastSql());
		}
	}	
	
	//单独更新论坛的标签统计
	function updataBbsTagCount(){
		$sql = "SELECT tags FROM `ts_bbs_topic` where tags <> ''";
		$list = D()->query($sql);
		foreach ($list as $v){
			$str .= $v['tags'].','; 
		}
		unset($list);
		
		$str = rtrim($str, ',');
		$str = explode(',', $str);
		$str = array_count_values($str);
				
		$key = array_keys($str);
		foreach ($key as $name){
			$tagname .= "'$name',"; 
		}
		$tagname = rtrim($tagname, ',');
		$map['tagname'] = array("IN", $tagname);
		$list = M('x_tag')->where($map)->field('tagid,tagname')->findall();
		foreach ($list as $vo){
			$name = $vo['tagname'];
			$data['bbscount']   = $str[$name];
			
			unset($map);
			$map['tagid'] = $vo['tagid'];
			$res = M('x_tag')->where($map)->save($data);
			//dump($res);
			//dump(M('x_tag')->getLastSql());
		}
	}		
	
}
?>